//
//  TestViewController.m
//  Proto
//
//  Created by kingly on 15/11/20.
//  Copyright © 2015年 Will. All rights reserved.
//

#import "TestViewController.h"

#import "NSObject+MKBlockTimer.h"
#import "ProtocolBuffers.h"
#import "Person.pb.h"
#import "People.pb.h"

@interface TestViewController (){

}
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    //序列化
    People_Builder  *testbuilder = [People builder];
    [testbuilder setName:@"libin"];
    [testbuilder setId:123456];
    [testbuilder setEmail:@"libin@163.com"];
    
    
    
   People *people1 = [testbuilder build];

  //反序列化
   People *people2 = [People parseFromData:people1.data];
    
    
   NSLog(@"people2：： %@ id: %d ",people2.name, people2.id);

    
    //序列化
    PersonBuilder *builder = [Person builder];
    
    NSArray *arr = @[[NSNumber numberWithUnsignedInteger:23],[NSNumber numberWithUnsignedInteger:25]];
    
    [builder setMyfieldArray:arr];
    
    Person *person = [builder build];
    
    NSLog(@"对象Person：： %@ ",person);
    
    NSAssert(([person.myfield uint32AtIndex:0] == [arr[0] unsignedIntegerValue]), @"NOT EQUALS");
    
    NSLog(@"%u",(unsigned int)[person.myfield uint32AtIndex:0]);
    
    
    //反序列化
    Person *person2 = [Person parseFromData:person.data];
    
    NSLog(@"%u",(unsigned int)[person2.myfield uint32AtIndex:1]);

    if ([person.myfield uint32AtIndex:1] == [person2.myfield uint32AtIndex:1]) {
        
        NSLog(@"OK");
        
    }
    
    ExampleObjectBuilder *object1 = [ExampleObject builder];
    [object1 setExampleFieldArray:@[@2.2,@2.3]];
    ExampleObject *object =[object1 build];
    NSLog(@"%@",object);
    
    [self connentServer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addClient:(id)sender;
{
    DomainClient *client = [[DomainClient alloc] initWithNibName:@"DomainClient" bundle:nil];
    [self.clients addObject:client];
    [client connectToUrl:self.server.url];
    [self.navigationController pushViewController:client animated:YES];

}

/**
 * 链接server
 **/
-(void)connentServer{

    _clients = [NSMutableArray new];
    
    NSError *error = nil;
    _server = [DomainServer new];
    self.server.url = [NSURL fileURLWithPath:@"/tmp/socket"];
    if (![self.server start:&error]) {
        NSLog(@" server start error");
    }
    
    [self addClient:nil];


}


@end
